<?php
namespace front\controllers;

use common\WebController;
use users\models\LoginForm;
use users\services\UserService;
use Yii;
use yii\filters\AccessControl;

/**
 * Site controller
 */
class SiteController extends WebController
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
//            'access' => [
//                'class' => AccessControl::className(),
//                'only' => ['login', 'logout', 'signup'],
//                'rules' => [
//                    [
//                        'allow' => true,
//                        'actions' => ['login', 'signup'],
//                        'roles' => ['?'],
//                    ],
//                    [
//                        'allow' => true,
//                        'actions' => ['logout'],
//                        'roles' => ['@'],
//                    ],
//                ],
//            ],
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


    public function actionTest()
    {
        print_r(32132);
    }

    public function actionIndex()
    {
        return $this->render('index');
    }


    /**
     *  Login
     * @return string|\yii\web\Response
     */
    public function actionLogin()
    {
        $model = new LoginForm();
        if (!Yii::$app->user->isGuest){
            return $this->redirect(Yii::$app->user->getReturnUrl());
        }
        if($model->load(Yii::$app->request->post())){
            $login = UserService::login($model->mobile, $model->password);
            if ($login['code'] == 0) {
                return $this->redirect(Yii::$app->user->getReturnUrl());
            }
        }
        return $this->render('login', ['model'=>$model]);
    }


}
